package com.example.recordingpen_datacollection.Utils.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.recordingpen_datacollection.Utils.AIUtil.AIChangeUtil;
import com.example.recordingpen_datacollection.Utils.DateUtils.TimeJudgeUtils;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.example.recordingpen_datacollection.Utils.http.HttpUrl.*;

public class BDokHttpUtil {



    public static String  getBDtoken() throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(BDurltoken)
                .build();
        try (Response response = client.newCall(request).execute()) {
           return (response.body().string());
        }
    }

    public static String  getBDtoken(String clientstr) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(BDHeadurltoken+clientstr)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return (response.body().string());
        }
    }

    public static String getBDData(String BDurl,String token,String role,String content) throws IOException {

//        System.out.println("开始当前请求时间-------------》"+ TimeJudgeUtils.getNowDate());
        String url = BDurl+token;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS) // 设置连接超时时间为30秒
                .readTimeout(120, TimeUnit.SECONDS) // 设置读取超时时间为60秒
                .writeTimeout(120, TimeUnit.SECONDS) // 设置写入超时时间为60秒
                .build();


        String jsonBody = "{ \"temperature\": 0.01,\"penalty_score\": 1 ,\"messages\": [{\"role\": \""+role+"\", \"content\": \""+content+"\"}]}";

        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).   execute()) {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                return responseData;
            } else {
                return response.message();
//                System.out.println("Request failed: " + ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getBDData(String BDurl,String token,String messages) throws IOException {
        String url = BDurl+token;
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS) // 设置连接超时时间为30秒
                .readTimeout(120, TimeUnit.SECONDS) // 设置读取超时时间为60秒
                .writeTimeout(120, TimeUnit.SECONDS) // 设置写入超时时间为60秒
                .build();

//        String jsonBody = "{\"messages\": [{\"role\": \""+role+"\", \"content\": \""+content+"\"}]}";
        String jsonBody = "{\"messages\": "+messages+",\"system\": \"小飞\",\"temperature\":0.5}";








        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                return responseData;
            } else {
                return response.message();
//                System.out.println("Request failed: " + ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getBDDataSet(String token,String role,String content,String system) throws IOException {

        String url =null;
        if( AIChangeUtil.judgeUtil(content) ){//&& false
            url = BDAIurlData_special+token;;
        } else {
            url = BDAIurlData+token;
        }

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(120, TimeUnit.SECONDS) // 设置连接超时时间为10秒
                .readTimeout(120, TimeUnit.SECONDS) // 设置读取超时时间为30秒
                .writeTimeout(120, TimeUnit.SECONDS) // 设置写入超时时间为30秒
                .build();
        Messages message = new Messages(role,content);
        List<Messages> msgList = new ArrayList<>();
        msgList.add(message) ;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("messages",msgList);
//            jsonObject.put("stream",true);
            jsonObject.put("system","飞利浦助手");//"Philips的AI助手"
            jsonObject.put("temperature",0.5);
//            jsonObject.put("temperature","0.2");
//            jsonObject.put("top_p",0.0);
            jsonObject.put("disable_search",false);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        String jsonBody = jsonObject.toJSONString();


        //{"disable_search":false,"messages":[{"content":"深圳天气怎么样？","role":"user"}],"stream":true,"system":"Philips的AI助手","temperature":0.5}
        RequestBody body = RequestBody.create(jsonBody, MediaType.parse("application/json"));

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String responseData = response.body().string();
                return responseData;
            } else {
                return response.message();
//                System.out.println("Request failed: " + ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}